package com.xxl.job.consume.repository;

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.consume.common.RedisKeyConstant;
import com.xxl.job.consume.model.DeviceDefModel;
import com.xxl.job.consume.utils.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.IOException;

/**
 * 查询设备信息
 */
@Repository
public class DeviceDefRepository {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 根据设备编码查询设备
     * @param devCode
     * @return
     */
    public DeviceDefModel query(String devCode) {
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.KEY_HASH_IOT_DEVICE);
        String sigDefModelStr=valueOperations.get(devCode);
        if (StringUtils.isNotBlank(sigDefModelStr)) {
            try {
                DeviceDefModel deviceInfoModel = JSONUtils.json2pojo(sigDefModelStr, DeviceDefModel.class);
                return deviceInfoModel;
            } catch (IOException e) {
                XxlJobLogger.log("终端JSON='{}' 转换异常！", sigDefModelStr, e);
            }
        }
        return null;
    }

}
